#!/usr/bin/env python
# -*- coding:utf-8 -*-

from bottle import Bottle
from bottle import request, response

from conn.MyPagerFactory import MyPager

memberpotAdm = Bottle()


@memberpotAdm.hook('before_request')
def validate():
    REQUEST_METHOD = request.environ.get('REQUEST_METHOD')
    HTTP_ACCESS_CONTROL_REQUEST_METHOD = request.environ.get('HTTP_ACCESS_CONTROL_REQUEST_METHOD')
    if REQUEST_METHOD == 'OPTIONS' and HTTP_ACCESS_CONTROL_REQUEST_METHOD:
        request.environ['REQUEST_METHOD'] = HTTP_ACCESS_CONTROL_REQUEST_METHOD


@memberpotAdm.hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'  # 允许所有域名的请求
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, DELETE, PUT, HEAD, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'origin,content-type,accept,authorization,x-requested-with'
    response.headers["Access-Control-Allow-Credentials"] = "true"
    response.headers['Access-Control-Max-Age'] = 86400
    response.status = 200
    

class MemberPointLevelRoute:
    ERROR_CODE = {
        "80": "数据读取异常错误，请联系管理员",
        "99": "处理参数错误",
        "991": "处理参数错误, 请填写 %s",
        "100": "数据添加失败，请检查服务端日志",
        "102": "数据更新失败，请检查服务端日志",
        "103": "数据删除失败，请检查服务端日志",
        "104": "数据停止失败，请检查服务端日志",
        "105": "数据恢复失败，请检查服务端日志",
        "106": "数据激活失败，请检查服务端日志",
        "109": "数据不存在，数据处理失败",
        "110": "关键数据重复，数据处理失败",
    }

    impl = None
    times_array = []

    def __init__(self, service):
        MemberPointLevelRoute.impl = service
        return

    @staticmethod
    @memberpotAdm.get('/fetch')
    def fetch():
        params = {"key": str(request.params.key).replace(" ", ""),
                  "pageSize": request.params.pageSize,
                  "pageNum": request.params.pageNum}
        res = MemberPointLevelRoute.impl.fetch(param=params)
        if res["code"] != 200:
            return {'code': 99, 'data': None, 'message': MemberPointLevelRoute.ERROR_CODE["99"]}
        view_list = res['data']
        _pages = MyPager.make_pager(id="table_members", res=view_list, page_size=int(params['pageSize']),
                                    data_total=res['total'], single_pager=False)
        return {'code': 200, 'data': _pages, 'message': ""}

    @staticmethod
    @memberpotAdm.get('/fetchOne')
    def fetch_one():
        memberId = request.params.id
        res = MemberPointLevelRoute.impl.fetch_one(id=memberId)
        if res["code"] != 200:
            return {'code': 99, 'data': None, 'message': MemberPointLevelRoute.ERROR_CODE["99"]}
        view = res['data'][0]
        return {'code': 200, 'data': view, 'message': ""}

    @staticmethod
    @memberpotAdm.post('/add')
    def add():
        if request.json is not None:
            data = request.json['data']
            res = MemberPointLevelRoute.impl.add_pointLevel_point(point_level=data)
            if res['code'] == 200:
                return {'code': 200, 'data': res, 'message': ""}
            else:
                return {'code': res['code'], 'data': res, 'message': MemberPointLevelRoute.ERROR_CODE["100"]}

    @staticmethod
    @memberpotAdm.post('/update')
    def update():
        if request.json is not None:
            data = request.json['data']
            id = request.json['id']
            res = MemberPointLevelRoute.impl.update_pointLevel_point(point_level=data, id=id)
            if res['code'] == 200:
                return {'code': 200, 'data': res, 'message': ""}
            elif res['code'] == 404:
                return {'code': res['code'], 'data': res, 'message': MemberPointLevelRoute.ERROR_CODE["109"]}
            else:
                return {'code': res['code'], 'data': res, 'message': MemberPointLevelRoute.ERROR_CODE["102"]}

    @staticmethod
    @memberpotAdm.get('/drop')
    def drop():
        memberId = request.params.id
        res = MemberPointLevelRoute.impl.drop_pointLevel_point(id=memberId)
        if res["code"] != 200:
            return {'code': 99, 'data': None, 'message': MemberPointLevelRoute.ERROR_CODE["103"]}
        elif res['code'] == 404:
            return {'code': res['code'], 'data': res, 'message': MemberPointLevelRoute.ERROR_CODE["109"]}
        return {'code': 200, 'data': None, 'message': ""}